// https : // iai.sh.cn/problem/610
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> f[i];
  for (int i = 1; i <= n; i++) {
    if (f[i] == i) {
      a[i] = i;
      continue;
    }
    if (!flag[i]) {
      flag[i] = true;
      cnt = 0;
      v.clear();
      memset(m, 0, sizeof(m));
      v.push_back(i);
      int idx = f[i];
      while (idx != i) {
        cnt++;
        m[idx] = cnt;
        value[cnt] = idx;
        v.push_back(idx);
        flag[idx] = true;
        idx = f[idx];
      }
      m[i] = ++cnt;
      value[cnt] = i;
      int move = k % cnt;
      for (int j = 0; j < v.size(); j++) {
        int now = m[v[j]];
        now = (now + move) % cnt;
        if (now == 0)
          now = cnt;
        a[value[now]] = v[j];
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';
  return 0;
}